﻿using MediatR;
using Microsoft.Extensions.Logging;
using SBlogCore.Application.Demo.Dto;
using SBlogCore.Application.Demo.Event;
using SBlogCore.Application.Demo.Event.PubCross;
using SBlogCore.Application.Demo.IServices;
using SBlogCore.Application.Demo.RemoteRequest;
using SBlogCore.Common.App;
using SBlogCore.Common.App.Options;
using SBlogCore.Common.CacheExtend;

namespace SBlogCore.Application.Demo.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [AllowAnonymous]
    public class DemoController : BaseController
    {
        private IMediator _mediator;
        private readonly IDemoService _demoservice;
        private readonly ILogger _logger;
        private readonly IHttpClientDemoService _clientDemoService;
        private readonly IPubCrossDemoEvent _pubCross;
        private readonly ISBlogCoreCache _cacheredis;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="demoservice"></param>
        /// <param name="logger"></param>
        /// <param name="eventBus"></param>
        /// <param name="clientDemoService"></param>
        /// <param name="pubCross"></param>
        /// <param name="cacheredis"></param>
        /// <param name="cachememory"></param>
        public DemoController(
            IMediator mediator,
            IDemoService demoservice,
            ILogger<DemoController> logger,
            IHttpClientDemoService clientDemoService,
            IPubCrossDemoEvent pubCross,
            ISBlogCoreCache cacheredis)
        {
            this._mediator = mediator;
            this._demoservice = demoservice;
            this._logger = logger;
            this._clientDemoService = clientDemoService;
            _pubCross = pubCross;
            this._cacheredis = cacheredis;
        }
        /// <summary>
        /// Demo测试
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultEntity<string> HelloDemo(string msg)
        {
            //AppBase使用
            var demos = AppBase.GetService<IDemoService>();
            //日志
            _logger.LogError("测试....");
            //获取当前用的用户信息
            //GetTokenUser();
            //通过IOption获取配置文件属性
            _ = AppsettingOptionsSingle.Data().Startup;
            //var res = await demos.HelloDemo();
            return Success("成功", "");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultEntity<string> HelloDemoAop(string msg)
        {
            var res =  _demoservice.HelloDemoAop(1,"xm",true);

            return Success("成功", res);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultEntity<string>> HelloDemoAsyncAop(string msg)
        {
            var res = await _demoservice.HelloDemoAopAsync(1,"xm",true);

            return Success("成功", res);
        }
        /// <summary>
        /// 事件队列总线
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task EventBusTest(string msg)
        {
            DemoOutPut outPut = new DemoOutPut()
            {
                Name = "Demo",
                Id = 1
            };
            await _pubCross.PublishTest(outPut,"测试");
        }
        /// <summary>
        /// 领域内事件传递
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task MediatorEventTest()
        {
           await _mediator.Publish(new DemoEvent("Test"));
        }
        /// <summary>
        /// 远程请求
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> GetHttpClientAction()
        {
            var res = await _clientDemoService.GetAction();
            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<string> GetParmAction()
        {
            var res = await _clientDemoService.GetParmAction("小明");
            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<DemoOutPut> PostHttpClientParmAction()
        {
            DemoOutPut outPut = new DemoOutPut()
            {
                Name = "小明",
                Id = 1234567777
            };
            var res = await _clientDemoService.PostParmAction(outPut,"XXXXXX-MMMMM-YYYY", _ => { 
            
            });
            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string TestDemoFunc()
        {
            return "测试";
        }
        /// <summary>
        /// 测试Redis
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> TestCache()
        {
            await _cacheredis.GetOrCreateAsync("TestRedis22", () => Task.FromResult(new { name="Test"}));
            var res = _cacheredis.Get<object>("TestRedis22");
            return res;
        }
        ///// <summary>
        ///// 测试Redis
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<dynamic> TestMemory()
        //{
        //    var dd = await _cachememory.GetOrCreateAsync("TestRedis22", () => Task.FromResult(new { name="Test"}));
        //    return dd;
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public string? FluentValidationDemo(DemoInput input)
        {
            return input.Name;
        }

    }
}
